package com.huaguoguo.sso.controller;

import com.huaguoguo.entity.User;
import com.huaguoguo.entity.vo.Result;
import com.huaguoguo.pojo.TbUser;
import com.huaguoguo.sso.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

/**
 * 类说明： <br/>
 * author:huaguoguo
 * Date: 2019/2/17
 * Time: 0:53
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String showRegister(Model model, String returnUrl) {

        model.addAttribute("uid", UUID.randomUUID().toString());

        return "register";
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String showLogin(Model model, String returnUrl) {

        model.addAttribute("returnUrl", returnUrl);


        return "login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Result showLogin(TbUser user) {

        Result login = loginService.login(user);

        return login;
    }

    @RequestMapping(value = "/success",method = RequestMethod.GET)
    public String showSuccess(String username, Model model) {

        model.addAttribute("username", username);

        return "success";
    }
}
